package com.qingcheng.controller.system;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.entity.PageResult;
import com.qingcheng.entity.Result;
import com.qingcheng.pojo.system.Admin;
import com.qingcheng.pojo.system.AdminRole;
import com.qingcheng.pojo.system.AdminRoles;
import com.qingcheng.service.system.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private HttpServletRequest authentication;

    private BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    @Reference
    private AdminService adminService;

    @GetMapping("/findAll")
    public List<Admin> findAll() {
        return adminService.findAll();
    }

    @GetMapping("/findPage")
    public PageResult<Admin> findPage(int page, int size) {
        return adminService.findPage(page, size);
    }

    @PostMapping("/findList")
    public List<Admin> findList(@RequestBody Map<String, Object> searchMap) {
        return adminService.findList(searchMap);
    }

    @PostMapping("/findPage")
    public PageResult<Admin> findPage(@RequestBody Map<String, Object> searchMap, int page, int size) {
        return adminService.findPage(searchMap, page, size);
    }

    @GetMapping("/findById")
    public AdminRoles findById(Integer id) {
        return adminService.findById(id);
    }


    @PostMapping("/add")
    public Result add(@RequestBody AdminRoles adminRoles) {
        Admin admin = adminRoles.getAdmin();
        String encode = passwordEncoder.encode(admin.getPassword());
        admin.setPassword(encode);
        adminService.add(adminRoles);
        return new Result();
    }

    @PostMapping("/update")
    public Result update(@RequestBody AdminRoles adminRoles) {
        Admin admin = adminRoles.getAdmin();
        if (admin.getPassword() != null) {
//            String encode = passwordEncoder.encode(admin.getPassword());
//            admin.setPassword(encode);
        }
        adminService.update(adminRoles);
        return new Result();
    }

    @GetMapping("/delete")
    public Result delete(Integer id) {
        adminService.delete(id);
        return new Result();
    }

    @PostMapping("modifyPassword")
    public Result modifyPassword(@RequestBody Map<String, String> map) {

        User currentAdmin = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String oldPassword = map.get("oldPassword");
        String loginName = currentAdmin.getUsername();
        Map hashMap = new HashMap<>();
        hashMap.put("loginName", loginName);
        List admins = adminService.findList(hashMap);
        Admin admin = (Admin) admins.get(0);
        System.out.println(oldPassword);
        System.out.println(admin.getPassword());
        boolean flag = passwordEncoder.matches(oldPassword, admin.getPassword());
        System.out.println(flag);
        if (flag) {
            String newPassword = passwordEncoder.encode(map.get("newPassword"));
            admin.setPassword(newPassword);
            AdminRoles adminRoles = adminService.findById(admin.getId());
            adminRoles.setAdmin(admin);
            adminService.update(adminRoles);
        } else {
            return new Result(1, "原密码错误！");
        }
        return new Result();
    }
}
